﻿$(function(){//延迟加载
	var parentId='';
	var newnode=null;
	var departmentId='';
	$('#dptree').tree({
		url:'getrootdp',
		onClick:function(node){
			departmentId=node.id;
			$('#ydpId').val(departmentId);
			$('#userlist').datagrid('reload',{
				departmentId:departmentId
			});
//			loaduserlist(departmentId);	
		},
		onContextMenu: function (e, node) {
//			alert($('#tabsMenu').html);
			$('#tabsMenu').menu();
	        e.preventDefault();
	        parentId=node.id; 
	        departmentId=node.id;
	        $('#tabsMenu').menu('show', {
	            left: e.pageX,
	            top: e.pageY
	        });
//	        alert($('#tabsMenu').html);
	    },
	    onExpand:function(node){
			if (node){
	    		var cnode=$('#dptree').tree('getChildren',node.target);
	    		//alert(cnode);
	    		if(cnode!=''){
//		    		alert(cnode[0].id);
		    		for(var i=0;i<cnode.length;i++){
		    			$('#dptree').tree('remove',cnode[i].target);
		    		}		    			
	    		}
				$.ajax({
					url:'extenddpnode?nodeId='+node.id,
					success:function(data){
						$('#dptree').tree('append', {
							parent: node.target,
							data:data
						});
					}
				});
			}
		}
	});
//	$('#dptree').tree({
//
//	});
	
	
	function adddg(parentId){		
		$('#adddg').dialog({
			title:'添加子部门',
			width:'330px',
			height:'225px',
			modal: true
		});
		$('#adddg').load('toadddepartment?parentId='+parentId,function(){
			$.getScript('/xasj/js/org/adddepartment.js');
		});
	}
	
	function addu(departmentId){		
		$('#auser').dialog({
			title:'添加人员',
			width:'330px',
			height:'320px',
			modal: true,
			collapsible:true
		});
		$('#auser').load('toadduser?departmentId='+departmentId,function(){
			$.getScript('/xasj/js/org/adduser.js');
		});
	}
	
	$('#addnewdepart').on('click',function(){
		adddg(parentId);
	});
	
	$('#addnewuser').on('click',function(){
		addu(departmentId);
	});
	
	$('#deletedepartment').click(function(){
		if(departmentId==''){
			return
		}
		$.ajax({
			url:'deletedepartment',
			type:'POST',
			data:{
				departmentId:departmentId
			},
			success:function(data){
				alert(data.msg);
				if(data.parentId!=undefined){
					var node=$('#dptree').tree('find',data.parentId);
					$('#dptree').tree('reload',node.target);
				}
			}
		});
	});
	
	$('#toeditdepartment').click(function(){
		if(departmentId==''){
			return
		}
		parent.addTab('编辑部门信息','toeditdepartment?departmentId='+departmentId);
	});
	
	$('#loaduserlist').load('touserlist',{
		
	},function(){
		
		$.getScript('/xasj/js/org/userlist.js');
	});
	
});